require(["common", "queryString", "coAjax", "href", "routie", './js/tools/getarea.js'], function(jSouper, QueryString, coAjax, href, routie) {
	routie({
		"*": function(hash) {
			var queryString = new QueryString(hash);
			var ach_id = queryString.get("ach_id");
			//获取订单信息
			coAjax.get(appConfig.user.ach_by_id + ach_id, function(result) {
				App.set("user.pay_ach", result.result);
				//订单取消
				App.set("user.pay_ach.$Event.cancel_ach_success", function(vm, result) {
					href.toMain();
				});
			}, function(errorCode) {
				console.log("errorCode:", errorCode);
				alert("error", "无效的订单编号");
			});
		}
	});
	App.set("$Event.pays.change_pay_method", function(e) {
		var pay_method = App.get("$Cache.pays.buy_method");
		coAjax.put(appConfig.user.ach_change_pay_method + App.get("user.pay_ach._id"), {
			pay_method: pay_method
		}, function(result) {
			App.set("user.pay_ach", result.result);
			alert("success", "支付方式修改成功");
		}, function(errorCode) {
			alert("error", "支付方式修改失败");
		})
	});
	//获取用户地址列表
	coAjax.get(appConfig.user.address_detail, function(result) {
		App.set("user.address_detail", result.result);
		App.getModel().touchOff("user.pay_ach.info.user_address")
	});
	App.set("$Event.add_user_address", function() {
		var new_user_address = {
			province: $("[name='province']").val(),
			town: $("[name='city']").val(),
			county: $("[name='area']").val(),
			// street:
			detail: $("[name='detailed_addre']").val(),
			mobilenumber: $("[name='mobile_num']").val(),
			phonenumber: $("[name='phone_num']").val(),
			postcode: $("[name='zip_code']").val(),
			name: $("[name='name']").val()
		};
		console.log(new_user_address);
		coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
			var address_detail = App.get("user.address_detail");
			address_detail.unshift(result.result);
			App.set("user.address_detail", address_detail);
			App.getModel().touchOff("user.pay_ach.info.user_address")
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});
	});
	//改变订单使用的收获地址
	App.set("$Event.pays.change_user_address", function(e, vm) {
		coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
			user_address_id: vm.get("_id")
		}, function(result) {
			App.set("user.pay_ach.info.user_address", result.result);
		});
	});
});